/*
 * EditNormalInvoiceDialog.java
 * business
 *
 * Created by ChenTao on 2017/1/18.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.utils.UIUtils;

/**
 * 编辑普通发票抬头对话框
 */
public class EditNormalInvoiceDialog extends Dialog {

    private EditText mEtTitle;

    private TextView mTvCancel;

    private TextView mTvSave;

    private OnConfirmListener mListener;

    public EditNormalInvoiceDialog(Context context) {
        super(context, R.style.ThemeNoTitleDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit_normal_invoice);

        initWindow();
        initViews();
        initListeners();
    }

    private void initWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        if (params != null) {
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(params);
        }
    }

    private void initViews() {
        mEtTitle = (EditText) findViewById(R.id.et_header);
        mTvCancel = (TextView) findViewById(R.id.tv_cancel);
        mTvSave = (TextView) findViewById(R.id.tv_save);
    }

    public void setUpdateStyle(String title) {
        mEtTitle.setText(title);
        mTvSave.setText(R.string.confirm);
    }

    private void initListeners() {
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mTvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(getInvoiceTitle())) {
                    UIUtils.showToast(UIUtils.getString(R.string.input_header_hint));
                    return;
                }

                dismiss();

                if (mListener != null) {
                    mListener.onConfirm(getInvoiceTitle());
                }
            }
        });
    }

    private String getInvoiceTitle() {
        return mEtTitle.getText().toString().trim();
    }

    public void setOnConfirmListener(OnConfirmListener listener) {
        mListener = listener;
    }

    public interface OnConfirmListener {
        void onConfirm(String invoiceTitle);
    }
}
